package com.foreknow.elm.controller;

import com.foreknow.elm.po.Cart;
import com.foreknow.elm.service.impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CartController {
    public Object saveCart(HttpServletRequest request){
        //获取前台请求参数  /CartController/saveCart?userId=1234567&businessId=10001&foodId=2
        CartServiceImpl cartService = new CartServiceImpl();
        int i = cartService.saveCart(request.getParameter("userId"),Integer.parseInt(request.getParameter("businessId")),Integer.parseInt(request.getParameter("foodId")));
        return i;
    }

    public Object listCart(HttpServletRequest request){
        //获取前台请求参数  /CartController/listCart?userId=12345671111&businessId=10001
        List<Cart> list = new ArrayList<>();
        CartServiceImpl cartService = new CartServiceImpl();
        Cart cart = new Cart();
        cart.setUserId(request.getParameter("userId"));
        cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        list = cartService.listCart(cart);
        return list;
    }

    public Object updateCart(HttpServletRequest request){
        //获取前台请求参数   /CartController/updateCart?quantity=5&userId=12345671111&businessId=10001&foodId=2
        CartServiceImpl cartService = new CartServiceImpl();
        int i = cartService.updateCart(request.getParameter("userId"),Integer.parseInt(request.getParameter("businessId")),Integer.parseInt(request.getParameter("foodId")),Integer.parseInt(request.getParameter("quantity")));
        return i;
    }

    public Object removeCart(HttpServletRequest request){
        //获取前台请求参数     /CartController/removeCart?userId=?&businessId=?
        // 获取前台请求参数    /CartController/removeCart?userId=?&businessId=?&foodId=?
        CartServiceImpl cartService = new CartServiceImpl();
        Cart cart = new Cart();
        cart.setUserId(request.getParameter("userId"));
        cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        cart.setFoodId(Integer.parseInt(request.getParameter("foodId")));
        int i = cartService.removeCart(cart);
        return i;
    }
}
